package com.anlu.base.async.task;

import com.anlu.base.exception.BaseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;


/**
 * <p>
 * Description:异步返回的对象
 * <p>
 * <p>
 *
 * @author anlu
 * @date 2024/9/23 10:02
 */
public class MyFuture<RR> implements Future<RR> {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    Future<RR> future;
    public MyFuture(Future<RR> future) {
        this.future = future;
    }

    @Override
    public boolean cancel(boolean mayInterruptIfRunning) {
        return future.cancel(mayInterruptIfRunning);
    }

    @Override
    public boolean isCancelled() {
        return future.isCancelled();
    }

    @Override
    public boolean isDone() {
        return future.isDone();
    }

    @Override
    public RR get() throws InterruptedException, ExecutionException {
        try{
            return future.get();
        } catch (Exception e){
            logger.error("{}",e.getClass(),e);
            //为了soner扫描
            logger.error(e.getMessage(), e);
            throw new BaseException(e);
        }
    }

    @Override
    public RR get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {

        try{
            return future.get(timeout,unit);
        } catch (Exception e){
            logger.error(e.getMessage(), e);
            throw new BaseException(e);
        }
    }
}
